<!--
 * 严肃声明：
 * 开源版本请务必保留此注释头信息，若删除我方将保留所有法律责任追究！
 * 本系统已申请软件著作权，受国家版权局知识产权以及国家计算机软件著作权保护！
 * 可正常分享和学习源码，不得用于违法犯罪活动，违者必究！
 * Copyright (c) 2020 陈尼克 all rights reserved.
 * 版权所有，侵权必究！
 *
-->
<template>
  <div class="address-box">
    <s-header :name="'企业认证'" back="/user"></s-header>
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <van-field
          v-model="state.form.name"
          name="公司名"
          :disabled="state.isAction === 'update'"
          label="公司名"
          placeholder="公司名"
          :rules="[{ required: true, message: '请填写公司名' }]"
        />

        <van-field
          v-model="state.form.foreign_name"
          name="公司英文名"
          label="公司英文名"
          placeholder="公司英文名"
        />
        <van-field
          v-model="state.form.city"
          name="地址"
          label="地址"
          placeholder="地址"
          :rules="[{ required: true, message: '请填写公司地址' }]"
        />

        <van-field name="logo2" label="logo">
          <template #input>
            <van-uploader
              v-model="state.form.logo2"
              max-count="1"
              :after-read="afterRead"
            />
          </template>
        </van-field>

        <van-field name="aaa" label="封面图">
          <template #input>
            <van-uploader
              v-model="state.form.poster2"
              max-count="1"
              :after-read="afterRead2"
            />
          </template>
        </van-field>
        <van-field name="businessLicense" label="营业执照">
          <template #input>
            <van-uploader
              v-model="state.form.businessLicense2"
              max-count="1"
              :after-read="afterRead3"
            />
          </template>
        </van-field>
        <van-field name="idCardFront" label="身份证正面">
          <template #input>
            <van-uploader
              v-model="state.form.idCardFront2"
              max-count="1"
              :after-read="afterRead4"
            />
          </template>
        </van-field>
        <van-field name="idCardOpposite" label="身份证背面">
          <template #input>
            <van-uploader
              v-model="state.form.idCardOpposite2"
              max-count="1"
              :after-read="afterRead5"
            />
          </template>
        </van-field>

        <van-field
          v-model="state.form.mission"
          name="公司使命"
          label="公司使命"
          placeholder="公司使命"
        />

        <van-field
          v-model="state.form.description"
          name="公司简介"
          label="公司简介"
          placeholder="公司简介"
        />

        <van-cell-group>
          <van-cell v-if="state.form.state" title="审核状态： 审核通过" />
          <van-cell v-else title="审核状态： 待审核" />
        </van-cell-group>
      </van-cell-group>
      <div style="margin: 16px">
        <van-button round block type="primary" native-type="submit">
          提交
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script setup>
import { reactive, onMounted } from "vue";
import sHeader from "@/components/SimpleHeader.vue";
import { uploadFile } from "@/service/good";
import { showToast } from "vant";
import {
  updateEnter,
  getCompanyById,
  addCompany,
  getCompanyByName,
  addHrCompany,
  updateState,
} from "@/service/enter";
import { getUserInfo } from "@/service/user";
import { useRoute, useRouter } from "vue-router";
const route = useRoute();
const router = useRouter();
const state = reactive({
  form: {
    name: "",
    foreign_name: "",
    city: "",
    mission: "",
    description: "",
    state: "",
    business_license: '',
    id_card_front: '',
    id_card_opposite: '',
  },
  user: "",
  loading: false,
  isAction: "",
});

onMounted(() => {
  init();
});

const init = async () => {
  let company = "";
  try {
    const data = await getUserInfo();
    state.user = data;

    company = await getCompanyById(state.user.id);
  } catch (err) {
    console.log(err);
    if (err.response.data.code === 10012) {
      showToast({
        message: "用户未登录",
        className: "particulars-detail-popup",
      });
      setTimeout(() => {
        router.back();
      }, 1500);
    }
  } finally {
    if (company && company.id !== "") {
      state.isAction = "update";
      state.form = company;
      state.form.logo2 = [
        {
          url: company.logo,
        },
      ];
      state.form.poster2 = [
        {
          url: company.poster,
        },
      ];
      state.form.businessLicense2 = [
        {
          url: company.business_license,
        },
      ];
      state.form.idCardFront2 = [
        {
          url: company.id_card_front,
        },
      ];
      state.form.idCardOpposite2 = [
        {
          url: company.id_card_opposite,
        },
      ];

    } else {
      state.isAction = "create";
    }
    state.loading = false;
  }
};

const afterRead = async (file) => {
  // 此时可以自行将文件上传至服务器

  const formdata = new FormData();
  formdata.append("file_0", file.file);

  const res = await uploadFile(formdata);

  if (res && res.length > 0) {
    state.form.logo = res[0].url;
  }
};
const afterRead2 = async (file) => {
  // 此时可以自行将文件上传至服务器

  const formdata = new FormData();
  formdata.append("file_0", file.file);

  const res = await uploadFile(formdata);

  if (res && res.length > 0) {
    state.form.poster = res[0].url;
  }
};
const afterRead3 = async (file) => {
  // 此时可以自行将文件上传至服务器

  const formdata = new FormData();
  formdata.append("file_0", file.file);

  const res = await uploadFile(formdata);

  if (res && res.length > 0) {
    state.form.business_license = res[0].url;
  }
};
const afterRead4 = async (file) => {
  // 此时可以自行将文件上传至服务器

  const formdata = new FormData();
  formdata.append("file_0", file.file);

  const res = await uploadFile(formdata);

  if (res && res.length > 0) {
    state.form.id_card_front = res[0].url;
  }
};
const afterRead5 = async (file) => {
  // 此时可以自行将文件上传至服务器

  const formdata = new FormData();
  formdata.append("file_0", file.file);

  const res = await uploadFile(formdata);

  if (res && res.length > 0) {
    state.form.id_card_opposite = res[0].url;
  }
};

const onSubmit = async () => {
  let res = "";
  try {
    if (state.isAction === "create") {
      await addCompany(state.form);

      // 根据新增的企业的名称查询company表，拿到对应的id
      const result = await getCompanyByName(state.form.name);

      if (result.code && result.code !== 3200) {
        showToast({
          message: result.message,
          className: "particulars-detail-popup",
        });
        return;
      }
      // 根据hr_id和company_id新增一条记录到hr_company表
      res = await addHrCompany(state.user.id, result.id);
    } else {
      res = await updateEnter(state.form.id, state.form);
      await updateState(state.form.id, 0);
    }

    showToast({
      message: "修改成功",
      className: "particulars-detail-popup",
    });
    router.back();
  } catch (error) {
    showToast({
      message: error.response.data.message,
      className: "particulars-detail-popup",
    });
  }
};
</script>

<style lang="less"></style>
